#-------------------------------------------------------#
#   Quality Assessment of the Academic Freedom Index    #
#-------------------------------------------------------#

# Title: "Quality Assessment of the Academic Freedom Index: Strengths, Weaknesses, and How Best to Use It" 
# Authors: "Lott, Lars", "Spannagel, Janika"
# date: 2024-09-19
# journal: Perspectives on Politics
# DOI: TBA
# written under "R and RStudio (4.4.1)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# load packages
library(here)
library(tidyverse)
library(rworldmap) # maps
library(magrittr)
library(reshape2)
library(ggpubr)
library(countrycode)
library(ggrepel)
library(ggthemes)
library(lubridate)
library(dotwhisker)
library(estimatr)
library(texreg)
library(readxl)
library(haven)
library(scales)
library(patchwork)    # For combining plots


#---------------------------------------------------#
#--------------------load data ---------------------#
#---------------------------------------------------#
#load coder-level data 
coder_level_ds <- read.csv(file.path("data/vdem_v13",  "vdem_13_coder_level", "coder_level_ds_v13.csv"))
gc()

## Inspect Confidence ratings ##

summary(coder_level_ds$v2cafres_conf)
head(coder_level_ds$v2cafres_conf)

ggplot(coder_level_ds, aes(x = v2cafres_conf)) +
  geom_density()


globalVariables(c("layer",  "GeomFlatViolin"))

geom_flat_violin <- function(mapping = NULL, data = NULL, stat = "ydensity",
                             position = "dodge", trim = TRUE, scale = "area",
                             show.legend = NA, inherit.aes = TRUE, ...) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomFlatViolin,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      trim = trim,
      scale = scale,
      ...
    )
  )
}


#' ggplot2-ggproto
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomFlatViolin <-
  ggproto("GeomFlatViolin", Geom,
          setup_data = function(data, params) {
            data$width <- data$width %||%
              params$width %||% (resolution(data$x, FALSE) * 0.9)
            
            # ymin, ymax, xmin, and xmax define the bounding rectangle for each group
            data %>%
              dplyr::group_by(group) %>%
              dplyr::mutate(
                ymin = min(y),
                ymax = max(y),
                xmin = x,
                xmax = x + width / 2
              )
          },
          
          draw_group = function(data, panel_scales, coord) {
            # Find the points for the line to go all the way around
            data <- transform(data,
                              xminv = x,
                              xmaxv = x + violinwidth * (xmax - x)
            )
            
            # Make sure it's sorted properly to draw the outline
            newdata <- rbind(
              plyr::arrange(transform(data, x = xminv), y),
              plyr::arrange(transform(data, x = xmaxv), -y)
            )
            
            # Close the polygon: set first and last point the same
            # Needed for coord_polar and such
            newdata <- rbind(newdata, newdata[1, ])
            
            ggplot2:::ggname("geom_flat_violin", GeomPolygon$draw_panel(newdata, panel_scales, coord))
          },
          
          draw_key = draw_key_polygon,
          
          default_aes = aes(
            weight = 1, colour = "grey20", fill = "white", size = 0.5,
            alpha = NA, linetype = "solid"
          ),
          
          required_aes = c("x", "y")
  )

# v2cafres_conf#

p1 <- coder_level_ds %>%
  drop_na(v2cafres)  %>% mutate(v2cafres = as.factor(v2cafres)) %>%
  ggplot(aes(x=v2cafres, y = v2cafres_conf, fill = v2cafres, colour = v2cafres))+
  geom_flat_violin(position = position_nudge(x = .25, y = 0),adjust =2, trim = FALSE)+
  geom_boxplot(aes(x = as.numeric(v2cafres), y = v2cafres_conf),outlier.shape = NA, alpha = 0.3, width = .1, colour = "BLACK") +
  ylab('v2cafres_conf')+ xlab('v2cafres') + coord_flip() + 
  labs(title = "v2cafres") +
  guides(fill = FALSE, colour = FALSE) +
  theme_pubr() + 
  #scale_x_discrete(labels=c("0" = "Democracy", "1" = "Autocracy"))+
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  scale_y_continuous(limits = c(0, 1))


# v2cafexch_conf # 

p2 <- coder_level_ds %>%
  drop_na(v2cafexch)  %>% mutate(v2cafexch = as.factor(v2cafexch)) %>%
  ggplot(aes(x=v2cafexch, y = v2cafexch_conf, fill = v2cafexch, colour = v2cafexch))+
  geom_flat_violin(position = position_nudge(x = .25, y = 0),adjust =2, trim = FALSE)+
  geom_boxplot(aes(x = as.numeric(v2cafexch), y = v2cafexch_conf),outlier.shape = NA, alpha = 0.3, width = .1, colour = "BLACK") +
  ylab('v2cafexch_conf')+ xlab('v2cafexch') + coord_flip() + 
  labs(title = "v2cafexch") +
  guides(fill = FALSE, colour = FALSE) +
  theme_pubr() + 
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  scale_y_continuous(limits = c(0, 1))

# v2cainsaut_conf #

p3 <- coder_level_ds %>%
  drop_na(v2cainsaut)  %>% mutate(v2cainsaut = as.factor(v2cainsaut)) %>%
  ggplot(aes(x=v2cainsaut, y = v2cainsaut_conf, fill = v2cainsaut, colour = v2cainsaut))+
  geom_flat_violin(position = position_nudge(x = .25, y = 0),adjust =2, trim = FALSE)+
  geom_boxplot(aes(x = as.numeric(v2cainsaut), y = v2cainsaut_conf),outlier.shape = NA, alpha = 0.3, width = .1, colour = "BLACK") +
  ylab('v2cainsaut_conf')+ xlab('v2cainsaut') + coord_flip() + 
  labs(title = "v2cainsaut") +
  guides(fill = FALSE, colour = FALSE) +
  theme_pubr() + 
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  scale_y_continuous(limits = c(0, 1))


# v2casurv_conf #

p4 <- coder_level_ds %>%
  drop_na(v2casurv)  %>% mutate(v2casurv = as.factor(v2casurv)) %>%
  ggplot(aes(x=v2casurv, y = v2casurv_conf, fill = v2casurv, colour = v2casurv))+
  geom_flat_violin(position = position_nudge(x = .25, y = 0),adjust =2, trim = FALSE)+
  geom_boxplot(aes(x = as.numeric(v2casurv), y = v2casurv_conf),outlier.shape = NA, alpha = 0.3, width = .1, colour = "BLACK") +
  ylab('v2casurv_conf')+ xlab('v2casurv') + coord_flip() + 
  labs(title = "v2casurv") +
  guides(fill = FALSE, colour = FALSE) +
  theme_pubr() + 
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  scale_y_continuous(limits = c(0, 1))

# v2clacfree_conf # 

p5 <- coder_level_ds %>%
  drop_na(v2clacfree)  %>% mutate(v2clacfree = as.factor(v2clacfree)) %>%
  ggplot(aes(x=v2clacfree, y = v2clacfree_conf, fill = v2clacfree, colour = v2clacfree))+
  geom_flat_violin(position = position_nudge(x = .25, y = 0),adjust =2, trim = FALSE)+
  geom_boxplot(aes(x = as.numeric(v2clacfree), y = v2clacfree_conf),outlier.shape = NA, alpha = 0.3, width = .1, colour = "BLACK") +
  ylab('v2clacfree_conf')+ xlab('v2clacfree') + coord_flip() + 
  labs(title = "v2clacfree") +
  guides(fill = FALSE, colour = FALSE) +
  theme_pubr() + 
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  scale_y_continuous(limits = c(0, 1))

# Combine plots #

p1 + p2 +p3 + p4+ p5 +
  plot_layout(ncol = 2) + 
  plot_annotation(tag_levels = 'A')
  plot_annotation(title = "Confidence of coders and respective indicators", 
                  theme =   theme_pubr() + theme(plot.subtitle = element_text(family = "Consolas")))

ggsave("outputs_original_data/Figure_I1.pdf", height = 30, width = 25, units = "cm", dpi = 1000)
ggsave("outputs_original_data/Figure_I1.png", height = 30, width = 25, units = "cm", dpi = 1000)
